install.packages("tidyverse")
library("tidyverse")Worksheet 2b
Importing, exploring and describing data
Timetable week: 5
Topic: "Measurement and Description"
Learning outcomes
By thee end of the session you will know how to:
- Install and load packages in R
- Import data into your R environment
- Explore variables of different types using descriptive statistics
- Create basic descriptive graphs to visualise your data
Introduction
In Worksheet 2a you have had a look at the RStudio environment and have created an R script (script.R or Lab_2.R) and a Quarto markdown document (Lab_2.qmd). In Worksheet 1 you have also explored some survey data sources and read through survey documentation to gain an understanding of how sociological concepts - such as “trust” - are being measured and operationalised in empirical research. The exercises on this worksheet will begin bringing these two activities together by exploring the original survey data using R.
Open the Lab_2.R script file and start working there. At the end, once you have completed all the exercises, the final task will be to transfer some of the code from the R script into the Lab_2.qmd markdown document, add some more description to what the code is supposed to achieve, and test if you have done it all correctly by rendering it to HTML or Microsoft Word.
Exercise B1: R functions and user-written packages
About 15 minutes
Most work in R is done using functions. The most common operations involving a function take the following generic form (think of an analogy of baking a loaf of bread):
It’s possible to create your own functions. This makes R extremely powerful and extendible. But instead of programming our own functions, we can rely on functions written by other people and bundled together into packages designed to perform some specific (or sometimes many very general) tasks.
There are a large number of reliable, tested and oft-used packages containing functions that are particularly useful for social scientists. In this module, we will rely on several such user-written packages that extend the basic packages already bundled in with our R software (the so-called base-R packages and functions).
Most mature packages are available from the Comprehensive R Archive Network (CRAN) or private repositories such as Bioconductor and GitHub. Packages made available on CRAN can be installed using the command install.packages("packagename"). Once the package/library is installed (i.e. it is sitting somewhere on your computer), we then need to load it to the current R session using the command library(packagename).
Install the tidyverse collection of packages and read about the packages and functions they contain
Type the following commands into your R script and execute each line of code:
Here’s a one-minute video crash-course on what you are expected to do: